<?php
namespace app\index\controller;
use app\index\model\Notice as NoticeModel;
use think\Request;
use app\index\model\Message as MessageModel;
use think\Db;


class Message extends Base
{
    //通知列表
    public function  messageList(Request $request)
    {
        $this -> view -> assign('title', '留言列表');
        $this -> view -> assign('keywords', '软件工程专业教学管理系统');
        $this -> view -> assign('desc', '软件工程专业教学管理系统');
		$d_id = $request -> param('id');//获取讨论ID
        $this -> view -> count = MessageModel::count();
        $list = MessageModel::where('d_id',$d_id)->paginate(10);

        $this -> view -> assign('list', $list);
        //渲染通知列表模板
        return $this -> view -> fetch('message_list');
    }

    //渲染编辑通知界面
    public function messageEdit(Request $request)
    {
        $message_id = $request -> param('id');
        $result = MessageModel::get($message_id);
        $this->view->assign('title','编辑留言');
        $this->view->assign('keywords','软件工程专业教学管理系统');
        $this->view->assign('desc','软件工程专业教学管理系统');
        $this->view->assign('message_info',$result->getData());
        return $this->view->fetch('message_edit');
    }

    //更新数据操作
    public function editMessage(Request $request)
    {
        $data = $request -> param();

        //设置更新条件
        $condition = ['id'=>$data['id']];

        //更新当前记录
        $result = MessageModel::update($data,$condition);

        //设置返回数据
        $status = 0;
        $message = '更新失败,请检查';

        //检测更新结果,将结果返回给notice_edit模板中的ajax提交回调处理
        if (true == $result) {
            $status = 1;
            $message = '恭喜, 更新成功~~';
        }
        return ['status'=>$status, 'message'=>$message];
    }

    //删除操作
    public function deleteMessage(Request $request)
    {
        $user_id = $request -> param('id');
        MessageModel::update(['is_delete'=>1],['id'=> $user_id]);
        MessageModel::destroy($user_id);

    }

    //恢复删除操作
    public function unDelete()
    {
        MessageModel::update(['delete_time'=>NULL],['is_delete'=>1]);
    }

    //添加操作的界面
    public function  messageAdd(Request $request)
    {
        $this->view->assign('title','添加留言');
        $this->view->assign('keywords','软件工程专业教学管理系统');
        $this->view->assign('desc','软件工程专业教学管理系统');
		$id = $request -> param('id');
		$this->view->assign('t_id',$id);
        return $this->view->fetch('message_add');
    }

    //添加操作
    public function addMessage(Request $request)
    {


        $data = $request -> param();
        $status = 0;
        $message = '添加失败';



        $rule = [
            'description|描述' => "require|min:3|max:20",
            'content|内容' => 'require|min:3'
        ];

        $result = $this -> validate($data, $rule);

        if ($result === true) {
            $user= MessageModel::create($request->param());
            if ($user != null) {
                $status = 1;
                $message = '添加成功~~';
            }
        }
        return ['status'=>$status, 'message'=>$message];
    }
}